package com.example.android.ouactivity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import com.example.android.ouactivity.adapter.SystemMsgAdapter;
import com.example.android.ouactivity.model.MessageSys;
import com.example.android.ouactivity.utils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统消息类
 * by chqing
 */
public class SystemMsgActivity extends AppCompatActivity {
    private List<MessageSys> datas = new ArrayList<>();
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = msg.obj.toString();
            Gson gson = new Gson();
            datas = gson.fromJson(result,new TypeToken<List<MessageSys>>(){}.getType());
            initView();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_msg);
        //设置返回，及页面标题
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (actionBar != null) {
            actionBar.setTitle("评论");
        }
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.getNewsJSON("http://www.chqing.cn/message",handler);
    }
    public void initView(){
        recyclerView = (RecyclerView)findViewById(R.id.sys_msg_view);
        layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        //添加分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        SystemMsgAdapter adapter = new SystemMsgAdapter(datas);
        recyclerView.setAdapter(adapter);
    }
    //左上角返回的监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
